home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2002 #3 / Amiga Plus CD - 2002 - No. 03.iso / AmigaPlus / Tools / Development / stunnel-4.04 / _src / src / log.c < prev    next >
Encoding:
C/C++ Source or Header  |  2003-01-01  |  5.1 KB  |  179 lines

  1. /*
  2.  *   stunnel       Universal SSL tunnel
  3.  *   Copyright (c) 1998-2003 Michal Trojnara <Michal.Trojnara@mirt.net>
  4.  *                 All Rights Reserved
  5.  *
  6.  *   This program is free software; you can redistribute it and/or modify
  7.  *   it under the terms of the GNU General Public License as published by
  8.  *   the Free Software Foundation; either version 2 of the License, or
  9.  *   (at your option) any later version.
  10.  *
  11.  *   This program is distributed in the hope that it will be useful,
  12.  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  *   GNU General Public License for more details.
  15.  *
  16.  *   You should have received a copy of the GNU General Public License
  17.  *   along with this program; if not, write to the Free Software
  18.  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  *
  20.  *   In addition, as a special exception, Michal Trojnara gives
  21.  *   permission to link the code of this program with the OpenSSL
  22.  *   library (or with modified versions of OpenSSL that use the same
  23.  *   license as OpenSSL), and distribute linked combinations including
  24.  *   the two.  You must obey the GNU General Public License in all
  25.  *   respects for all of the code used other than OpenSSL.  If you modify
  26.  *   this file, you may extend this exception to your version of the
  27.  *   file, but you are not obligated to do so.  If you do not wish to
  28.  *   do so, delete this exception statement from your version.
  29.  */
  30.  
  31. #include "common.h"
  32. #include "prototypes.h"
  33.  
  34. static FILE *outfile=NULL; /* Logging to file disabled by default */
  35.  
  36. #if defined (USE_WIN32) || defined (__vms)
  37.  
  38. /* HANDLE evt=NULL; */
  39.  
  40. void log_open(void) { /* Win32 version */
  41. #if 0
  42.     AllocConsole();
  43.     /* reopen stdin handle as console window input */
  44.     freopen("CONIN$", "rb", stdin);
  45.     /* reopen stout handle as console window output */
  46.     freopen("CONOUT$", "wb", stdout);
  47.     /* reopen stderr handle as console window output */
  48.     freopen("CONOUT$", "wb", stderr);
  49.     printf("Close this window to exit stunnel\n\n");
  50. #endif
  51.     if(options.output_file)
  52.         outfile=fopen(options.output_file, "a");
  53.     if(outfile)
  54.         return; /* It was possible to open a log file */
  55.     /* TODO: Register NT EventLog source here */
  56.     /* evt=RegisterEventSource(NULL, "stunnel"); */
  57.     if(options.output_file)
  58.         log(LOG_ERR, "Unable to open output file: %s", options.output_file);
  59. }
  60.  
  61. void log_close(void) {
  62.     if(outfile)
  63.         fclose(outfile);
  64. #if 0
  65.     else
  66.         FreeConsole();
  67. #endif
  68. }
  69.  
  70. #else /* USE_WIN32, __vms */
  71.  
  72. void log_open(void) { /* Unix version */
  73.     int fd;
  74.  
  75.     if(options.output_file) { /* 'output' option specified */
  76.         fd=open(options.output_file, O_CREAT|O_WRONLY|O_APPEND, 0640);
  77.         if(fd>=0) { /* file opened or created */
  78.             fcntl(fd, F_SETFD, FD_CLOEXEC);
  79.             outfile=fdopen(fd, "a");
  80.             if(outfile)
  81.                 return; /* no need to setup syslog */
  82.         }
  83.     }
  84.     if(options.option.syslog) {
  85. #ifdef __ultrix__
  86.         openlog("stunnel", LOG_PID);
  87. #else
  88.         openlog("stunnel", LOG_CONS | LOG_NDELAY | LOG_PID, options.facility);
  89. #endif /* __ultrix__ */
  90.     }
  91.     if(options.output_file)
  92.         log(LOG_ERR, "Unable to open output file: %s", options.output_file);
  93. }
  94.  
  95. void log_close(void) {
  96.     if(outfile) {
  97.         fclose(outfile);
  98.         return;
  99.     }
  100.     if(options.option.syslog)
  101.         closelog();
  102. }
  103.  
  104. #endif /* USE_WIN32, __vms */
  105.  
  106. void log(int level, const char *format, ...) {
  107.     va_list arglist;
  108.     char text[STRLEN], timestamped[STRLEN];
  109.     FILE *out;
  110.     time_t gmt;
  111.     struct tm *timeptr;
  112. #ifdef HAVE_LOCALTIME_R
  113.     struct tm timestruct;
  114. #endif
  115.  
  116.     if(level>options.debug_level)
  117.         return;
  118.     va_start(arglist, format);
  119. #ifdef HAVE_VSNPRINTF
  120.     vsnprintf(text, STRLEN, format, arglist);
  121. #else
  122.     vsprintf(text, format, arglist);
  123. #endif
  124.     va_end(arglist);
  125. #if !defined (USE_WIN32) && !defined (__vms)
  126.     if(!outfile && options.option.syslog) {
  127.         syslog(level, "%s", text);
  128.         return;
  129.     }
  130. #endif /* USE_WIN32, __vms */
  131.     out=outfile?outfile:stderr;
  132.     time(&gmt);
  133. #ifdef HAVE_LOCALTIME_R
  134.     timeptr=localtime_r(&gmt, ×truct);
  135. #else
  136.     timeptr=localtime(&gmt);
  137. #endif
  138. #ifdef HAVE_SNPRINTF
  139.     snprintf(timestamped, STRLEN,
  140. #else
  141.     sprintf(timestamped,
  142. #endif
  143.         "%04d.%02d.%02d %02d:%02d:%02d LOG%d[%lu:%lu]: %s",
  144.         timeptr->tm_year+1900, timeptr->tm_mon+1, timeptr->tm_mday,
  145.         timeptr->tm_hour, timeptr->tm_min, timeptr->tm_sec,
  146.         level, stunnel_process_id(), stunnel_thread_id(), text);
  147. #ifdef USE_WIN32
  148.     win_log(timestamped); /* Always log to the GUI window */
  149.     if(outfile) /* to the file - only if it exists */
  150. #endif
  151.     {
  152.         fprintf(out, "%s\n", timestamped);
  153.         fflush(out);
  154.     }
  155. }
  156.  
  157. void log_raw(const char *format, ...) {
  158.     va_list arglist;
  159.     char text[STRLEN];
  160.     FILE *out;
  161.  
  162.     va_start(arglist, format);
  163. #ifdef HAVE_VSNPRINTF
  164.     vsnprintf(text, STRLEN, format, arglist);
  165. #else
  166.     vsprintf(text, format, arglist);
  167. #endif
  168.     va_end(arglist);
  169.     out=outfile?outfile:stderr;
  170. #ifdef USE_WIN32
  171.     win_log(text);
  172. #else
  173.     fprintf(out, "%s\n", text);
  174.     fflush(out);
  175. #endif
  176. }
  177.  
  178. /* End of log.c */
  179.